//index.js
//获取应用实例
const app = getApp()
const nt_util = require('../../utils/nt_util.js')

Page({
  data: {
    data:[],
    curIndex: 6,
    storeIndex: 0,
    typeId: '',
    storeTypeList: [],
    storeTypeListOrigin: [],
    items:[],
    hasMore:true,
    once: false,
    loadding:false,
    latitude:'',
    longitude:'',
    limit:10,
    offset:0,
    listitems:[]
  },

  onLoad: function () {
    nt_util.that = this;
    // nt_util.style();
    nt_util.globalData = app.globalData;

    //1、获取当前位置坐标
    wx.getLocation({
      type: 'gcj02',// 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        // nt_util.log(res);
        nt_util.that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        nt_util.saveUserLocation(res.latitude, res.longitude)
      },
      complete: function (e) {
        nt_util.that.getHome();
      }
    })

    nt_util.that.getStoreTypeList();
    nt_util.that.getStore();
    
  },

  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    wx.getStorage({
      key: 'home_reload',
      success: function (res) {
        // console.log(res.data)
        if (res.data == 'true'){
          wx.removeStorage({
            key: 'home_reload',
            success: function (res) {
              // console.log(res.data)
            }
          })

          nt_util.that.setData({
            storeIndex: 0,
            typeId: '',
            offset: 0
          })

          //1、获取当前位置坐标
          wx.getLocation({
            type: 'gcj02',// 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            success: function (res) {
              nt_util.that.setData({
                latitude: res.latitude,
                longitude: res.longitude
              })
              nt_util.saveUserLocation(res.latitude, res.longitude)
            },
            complete: function (e) {
              nt_util.that.getHome();
            }
          })
        }
      
      }
    })
  },
  

  // 获取店铺类型列表
  getStoreTypeList: function () {
    var that = this;
    nt_util.httpRequest('api/life/v1/getStoreTypeList', 'post', {}, function (res) {
      var _list = res.data.list || [];
      var _storeTypeList = [];
      for (var i = 6; i < _list.length; i++) {
        _storeTypeList.push(_list[i].name);
        // that.data.storeTypeList.push(_list[i].name);//这样不会绑定数据, setData 会双向绑定数据
      }
      that.setData({
        
        storeTypeList: _storeTypeList,
        storeTypeListOrigin: _list,
        // typeId: _list[0].id
      });
    });
  },
    getStore: function () {
      nt_util.httpRequest('api/life/v1/getStoreTypeList', 'post', {}, function (data) {
        let _listitems = []
        let _listitems2 = []
        let _list = data.data.list
        if (_list){
          let _length = data.data.list.length
          if (0 < _length && _length<=4){
            _listitems = _list
          } else if (4 < _length && _length<=8){
            for (var i = 0; i < _length;i++){
              if(i<4){
                _listitems.push(_list[i])
              }else{
                _listitems2.push(_list[i])
              }
            }
          }else{
            for (var i = 0; i < _length; i++) {
              if (i < _length/2) {
                _listitems.push(_list[i])
              } else {
                _listitems2.push(_list[i])
              }
            }
          }
        }
        nt_util.that.setData({
          listitems: [_listitems, _listitems2],
          once: true,
        })

      });
    },
    siwtchTap(e) {
      const self = this;
      this.setData({
        curIndex: e.currentTarget.dataset.index,
        todo: e.currentTarget.dataset.name + '+'
      })

      var _i = e.currentTarget.dataset.index
      if (_i == 6){
        this.setData({
          typeId: '',
          offset: 0
        })
        wx.setNavigationBarTitle({
          title: '全部'
        })
        this.getHome();
      } else if (_i == 7){

      }else{
        var _typeId = this.data.storeTypeListOrigin[e.currentTarget.dataset.index].id;
        this.setData({
          typeId: _typeId,
          offset: 0
        })

        wx.setNavigationBarTitle({
          title: this.data.storeTypeListOrigin[e.currentTarget.dataset.index].name
        })

        this.getHome();
      }
      
    },
  getHome: function (e) {
    nt_util.that = this;
    nt_util.globalData = app.globalData;
    wx.showLoading({
      title: '加载中...',
    })

    // 获取轮播图
    nt_util.httpRequest('api/life/v1/getSowingMapListByAppid', 'POST', {
      
    }, function (data) {
      nt_util.that.setData({
        data: data.data.list,
      });
      // nt_util.nt_log(nt_util.that.data.data);
    });

    // 获取列表数据
    nt_util.httpRequest('api/life/v1/getStoreInfoList', 'POST', {
      typeId: nt_util.that.data.typeId,
      lng: nt_util.that.data.longitude,
      lat: nt_util.that.data.latitude,
      limit: nt_util.that.data.limit,
      offset: nt_util.that.data.offset
    }, function (data) {
      nt_util.that.setData({
        items: data.data.list,
        loadding: false,
        offset: nt_util.that.data.offset + nt_util.that.data.limit
      });

      //判断是否还有数据
      if (data.data.list.length < nt_util.that.data.limit) {
        nt_util.that.setData({
          hasMore: false
        });
      }
      // nt_util.nt_log(nt_util.that.data.data);
    });
  },

  bindPickerChange:function(e){
    var _storeIndex = parseInt(e.detail.value) + 6;
    var _typeId = this.data.storeTypeListOrigin[_storeIndex].id;
    this.setData({
      storeIndex: _storeIndex,
      typeId: _typeId,
      offset:0
    })

    

    this.getHome();
  },

  //快速拨打电话号码
  quickCall: function (event) {
    var phone = event.currentTarget.dataset.phone;
    //判断电话号码是否可用
    if (phone != null) {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }

  },

  loadMore: function (event) {
    if ((!this.data.loadding) && this.data.hasMore) {

      //防止多次触底 触发多次
      this.setData({
        loadding: true
      });

      nt_util.httpRequest('api/life/v1/getStoreInfoList', 'POST', {
        typeId: nt_util.that.data.typeId,
        lng: nt_util.that.data.longitude,
        lat: nt_util.that.data.latitude,
        limit: nt_util.that.data.limit,
        offset: nt_util.that.data.offset
      }, function (data) {
        var _items = nt_util.that.data.items;
        _items = _items.concat(data.data.list);
        nt_util.that.setData({
          items: _items,
          loadding: false,
          offset: nt_util.that.data.offset + nt_util.that.data.limit
        });

        //判断是否还有数据
        if (data.data.list.length < nt_util.that.data.limit) {
          nt_util.that.setData({
            hasMore: false
          });
        }
      });
    }
  },
  scroll: function (event) {

  },

  onShareAppMessage: function () {
    
  },

  //点击跳转到详细页面
  tapItem: function (event) {

    var dataset = event.currentTarget.dataset;
    //拿到点击的index下标
    var index = dataset.index
    // var queryObj = nt_util.deleteEmptyProperty(nt_util.that.data.items[index]);
    var _queryObj =this.data.items[index];
    var queryObj = { 
      store_pic: _queryObj.store_pic,
      name: _queryObj.name,
      store_name: _queryObj.store_name,
      phone: _queryObj.phone,
      phone_bak: _queryObj.phone_bak,
      address: _queryObj.address,
      lat: _queryObj.lat,
      lng: _queryObj.lng,
      remark: _queryObj.remark
      }
    //将对象转为string
    var queryBean = JSON.stringify(queryObj)
    var url = '/pages/index/newd?queryBean=' + queryBean;
    
    // var ary = new Array();
    // if (dataset.fdname != null) {
    //   ary.push("fdBaseName=" + dataset.fdname);
    // }

    // if (dataset.fdaddress != null) {
    //   ary.push("fdAddress=" + dataset.fdaddress);
    // }

    // if (dataset.fdheadteacher != null) {
    //   ary.push("fdHeadTeacher=" + dataset.fdheadteacher);
    // }

    // if (dataset.fdheadteacherphone != null) {
    //   ary.push("fdHeadTeacherPhone=" + dataset.fdheadteacherphone);
    // }

    // if (dataset.paintingphotos.length > 0) {
    //   ary.push("paintingPhotos=" + dataset.paintingphotos);
    // }

    // if (dataset.basephotos.length > 0) {
    //   ary.push("basePhotos=" + dataset.basephotos);
    // }


    // if (dataset.fdlongitude != null) {
    //   ary.push("fdLongitude=" + dataset.fdlongitude);
    // }

    // if (dataset.fdlatitude != null) {
    //   ary.push("fdLatitude=" + dataset.fdlatitude);
    // }


    // if (dataset.fdbasethumbimg != null) {
    //   ary.push("fdBaseThumbImg=" + dataset.fdbasethumbimg);
    // }

    // ary.forEach(function (val, index, ary) {
    //   url += val + '&';
    // });
    // url = url.substring(0, url.lastIndexOf('&'));

    wx.navigateTo({
      url: url
    })
  },

  // showInput: function () {
  //   this.setData({
  //     inputShowed: true
  //   });
  // },
  // hideInput: function () {
  //   this.setData({
  //     inputVal: "",
  //     inputShowed: false
  //   });
  // },
  // clearInput: function () {
  //   this.setData({
  //     inputVal: ""
  //   });
  // },
  // inputTyping: function (e) {
  //   this.setData({
  //     inputVal: e.detail.value
  //   });
  // }
})
